﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Service.ChapterServices;
using HZYProject.Service.CourseServices;
using HZYProject.Service.View_ChapterCourseServices;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    /// <summary>
    /// 章节管理
    /// </summary>
    [Route("Chapter")]
    public class ChapterController : Controller
    {
        
        private readonly IRepository<Chapter, Guid> _repository;
        private readonly IRepository<Course, Guid> _courseRepository;
        private readonly IChapterService _chapterService;
        private readonly IView_ChapterCourseService _chapterCourseService;

        public ChapterController(IRepository<Chapter, Guid> repository, IChapterService chapterService, IRepository<Course, Guid> courseRepository, IView_ChapterCourseService chapterCourseService)
        {
            _repository = repository;
            _chapterService = chapterService;
            _courseRepository = courseRepository;
            _chapterCourseService = chapterCourseService;
        }

        #region 首页分页查询
        [HttpGet("")]
        public IActionResult Index()
        {
            var list = _courseRepository.GetAllList();
            return View(list);
        }
        //[HttpPost("")]
        //public async Task<IActionResult> Index(View_ChapterCourse c)
        //{
        //    try
        //    {
        //        GetChapterInput input = new GetChapterInput { ID = c.ChapterID, Title = c.Title, CID = c.CourseID };
        //        var data = await _chapterCourseService.GetPaginatedResult(input);
        //        return Json(new { code = 0, msg = "", data = data.Data, count = data.TotalCount });
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { code = 1, msg = "查询出错:" + ex.Message, data = new List<View_ChapterCourse>(), count = 0 });
        //    }
        //}
        [HttpPost("")]
        public async Task<IActionResult> Index(UIChapter c)
        {
            try
            {
                GetChapterInput input = new GetChapterInput { ID = c.ChapterID, Title = c.Title, CID = c.CourseID };
                var data = await _chapterService.GetPaginatedResult(input);
                var list = data.Data.Select(x => new UIChapter
                {
                    ChapterID = x.ChapterID,
                    Title = x.Title,
                    CourseID = x.CourseID,
                    CreateTime = x.CreateTime,
                    CourseName = x.Course.Title
                });
                return Json(new { code = 0, msg = "", data = list, count = data.TotalCount });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询出错:" + ex.Message, data = new List<UIChapter>(), count = 0 });
            }
        }
        #endregion

        #region 添加
        [HttpGet("add")]
        public IActionResult Add()
        {
            var list = _courseRepository.GetAllList();
            return View(list);
        }
        [HttpPost("add")]
        public IActionResult Add(UIChapter cla)
        {
            try
            {
                Chapter c = new Chapter();
                c.ChapterID = Guid.NewGuid();
                c.Title = cla.Title;
                c.CourseID = cla.CourseID;
                c.CreateTime = DateTime.Now;
                _repository.Insert(c);
                return Json(new { success = true, msg = "添加成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion

        #region 修改
        [HttpGet("edit")]
        public IActionResult Edit(Guid id)
        {
            var obj = _repository.FirstOrDefault(x => x.ChapterID == id);
            ViewBag.Names = _courseRepository.GetAllList();
            return View(obj);
        }
        [HttpPost("edit")]
        public IActionResult Edit(UIChapter cla)
        {
            try
            {
                if (ModelState.IsValid)//obj在形成对象时，是否全部符合注解中的规定
                {
                    var obj = _repository.FirstOrDefault(x => x.ChapterID == cla.ChapterID);
                    obj.Title = cla.Title;
                    obj.CourseID = cla.CourseID;
                    _repository.Update(obj);
                    return Json(new { success = true, msg = "修改成功" });
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var v in ModelState.Values)
                    {
                        foreach (var p in v.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new { success = false, msg = "服务端验证未通过," + sb });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }

        }
        #endregion

        #region 删除
        [HttpPost("remove/{id}")]
        public IActionResult Del(Guid id)
        {
            try
            {
                var obj = _repository.FirstOrDefault(x => x.ChapterID == id);
                _repository.Delete(obj);
                return Json(new { success = true, msg = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion
    }
}
